import React from 'react';
import ShowMsg from './ShowMsg';
import HrefUrl from './HrefUrl';
import axios from 'axios';

const apiUrl = './getData?id=1';

class GetData extends React.Component {
    constructor(props) {
        super(props);
        this.state = { "dataId": "0" };
    }

    onClick = () => {
        axios({ method: 'get', url: `${apiUrl}` })
          .then(response => {
            console.log(response);
            var data = response.data;
            console.log(data.errorCode);
    
            if (data.errorCode === 0) {
              console.log(data);
              this.setState(function (state) {
    
                state.dataId = data.id;
                return { "dataId": state.dataId };
              });
            }
          });
      }

    render() {
        return (
            <div>
                <HrefUrl url="#" target="_self" title="Get Data" onClick={this.onClick} />
                <ShowMsg dataId={this.state.dataId} />
            </div>
        );
    }
}

export default GetData;